package com.ruoyi.insect.service.impl;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.insect.domain.DiseaseInfo;
import com.ruoyi.insect.mapper.DiseaseInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.insect.mapper.DiseaseLabelMapper;
import com.ruoyi.insect.domain.DiseaseLabel;
import com.ruoyi.insect.service.IDiseaseLabelService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 数据标注Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-26
 */
@Service
public class DiseaseLabelServiceImpl implements IDiseaseLabelService 
{
    @Autowired
    private DiseaseLabelMapper diseaseLabelMapper;

    @Autowired
    private DiseaseInfoMapper diseaseInfoMapper;

    @Transactional(rollbackFor = Exception.class)  // 事务管理

    /**
     * 查询数据标注
     * 
     * @param id 数据标注主键
     * @return 数据标注
     */
    @Override
    public DiseaseLabel selectDiseaseLabelById(Long id)
    {
        return diseaseLabelMapper.selectDiseaseLabelById(id);
    }

    /**
     * 查询数据标注列表
     * 
     * @param diseaseLabel 数据标注
     * @return 数据标注
     */
    @Override
    public List<DiseaseLabel> selectDiseaseLabelList(DiseaseLabel diseaseLabel)
    {
        return diseaseLabelMapper.selectDiseaseLabelList(diseaseLabel);
    }

    /**
     * 新增数据标注
     * 
     * @param diseaseLabel 数据标注
     * @return 结果
     */
    @Override
    public int insertDiseaseLabel(DiseaseLabel diseaseLabel)
    {
        if(StringUtils.isEmpty(diseaseLabel.getDiseaseName())){
            throw new BusinessException("病虫害名称不能为空");
        }

        int rows=diseaseLabelMapper.insertDiseaseLabel(diseaseLabel);

        syncToDiseaseInfo(diseaseLabel);

        return rows;
    }

    private void syncToDiseaseInfo(DiseaseLabel label){
        DiseaseInfo existDisease =diseaseInfoMapper.selectDiseaseInfoByName(label.getDiseaseName());

        if(existDisease==null){
            DiseaseInfo newDisease=new DiseaseInfo();
            newDisease.setName(label.getDiseaseName());
            newDisease.setSymptom(label.getDiseaseSymptom());
            newDisease.setSolution(label.getDiseaseSolution());
            newDisease.setImageUrl(label.getOriginalImage());
            diseaseInfoMapper.insertDiseaseInfo(newDisease);
        }else{
            String updateSolution=existDisease.getSolution()+"\n\n[补充方案]"+label.getDiseaseSolution();
            existDisease.setSolution(updateSolution);

            existDisease.setSymptom(label.getDiseaseSymptom());

            existDisease.setImageUrl(label.getOriginalImage());
            diseaseInfoMapper.updateDiseaseInfo(existDisease);
        }
    }

    /**
     * 修改数据标注
     * 
     * @param diseaseLabel 数据标注
     * @return 结果
     */
    @Override
    public int updateDiseaseLabel(DiseaseLabel diseaseLabel)
    {
        return diseaseLabelMapper.updateDiseaseLabel(diseaseLabel);
    }

    /**
     * 批量删除数据标注
     * 
     * @param ids 需要删除的数据标注主键
     * @return 结果
     */
    @Override
    public int deleteDiseaseLabelByIds(Long[] ids)
    {
        return diseaseLabelMapper.deleteDiseaseLabelByIds(ids);
    }

    /**
     * 删除数据标注信息
     * 
     * @param id 数据标注主键
     * @return 结果
     */
    @Override
    public int deleteDiseaseLabelById(Long id)
    {
        return diseaseLabelMapper.deleteDiseaseLabelById(id);
    }
}
